/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.business.service.impl;

import me.zhengjie.modules.business.constant.EnableEnum;
import me.zhengjie.modules.business.constant.ExStatusEnum;
import me.zhengjie.modules.business.domain.ExperimentDO;
import me.zhengjie.modules.business.domain.SimpleExperimentDO;
import me.zhengjie.modules.business.repository.SimpleExperimentRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.ExperimentRepository;
import me.zhengjie.modules.business.service.ExperimentService;
import me.zhengjie.modules.business.service.dto.ExperimentDto;
import me.zhengjie.modules.business.service.dto.ExperimentQueryCriteria;
import me.zhengjie.modules.business.service.mapstruct.ExperimentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ljj
* @date 2021-02-19
**/
@Service
@RequiredArgsConstructor
public class ExperimentServiceImpl implements ExperimentService {

    private final ExperimentRepository experimentRepository;
    private final ExperimentMapper experimentMapper;
    private final SimpleExperimentRepository simpleExperimentRepository;

    @Override
    public Map<String,Object> queryAll(ExperimentQueryCriteria criteria, Pageable pageable){
        Page<ExperimentDO> page = experimentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(experimentMapper::toDto));
    }

    @Override
    public Map<String,Object> queryAllSimple(ExperimentQueryCriteria criteria, Pageable pageable){
        Page<SimpleExperimentDO> page = simpleExperimentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page);
    }

    @Override
    public List<SimpleExperimentDO> queryAllSimple(ExperimentQueryCriteria criteria){
        return simpleExperimentRepository.findAll((root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
    }
    @Override
    public List<ExperimentDto> queryAll(ExperimentQueryCriteria criteria){
        return experimentMapper.toDto(experimentRepository.findAll((root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ExperimentDto findById(String id) {
        ExperimentDO experiment = experimentRepository.findById(id).orElseGet(ExperimentDO::new);
        ValidationUtil.isNull(experiment.getId(),"Experiment","id",id);
        return experimentMapper.toDto(experiment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExperimentDto create(ExperimentDO resources) {
        resources.init();
        return experimentMapper.toDto(experimentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExperimentDO resources) {
        ExperimentDO experiment = experimentRepository.findById(resources.getId()).orElseGet(ExperimentDO::new);
        ValidationUtil.isNull( experiment.getId(),"Experiment","id",resources.getId());
        experiment.copy(resources);
        experimentRepository.save(experiment);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            experimentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ExperimentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExperimentDto experiment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", experiment.getName());
            map.put("类型", experiment.getType());
            map.put("描述", experiment.getRemark());
            map.put("状态", experiment.getStatus());
            map.put("流量层id", experiment.getLayerId());
            map.put("软删除", experiment.getSoftDelete());
//            map.put(" createBy",  experiment.getC());
//            map.put(" updateBy",  experiment.getUp());
            map.put(" createTime",  experiment.getCreateTime());
//            map.put(" updateTime",  experiment.getUpdateTime());
            map.put("默认连接", experiment.getDefaultLink());
            map.put("匹配模式", experiment.getPatternType());
            map.put("默认变量名", experiment.getDefaultVar());
            map.put("应用ID", experiment.getAppKey());
            map.put("实验key", experiment.getId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}